package com.hcl.orm.core.spring;

import com.hcl.orm.core.executor.DaoMethodExecutor;
import org.springframework.jdbc.core.JdbcOperations;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hucl
 * @projectName jim-orm
 * @description: TODO
 * @date 2021/5/1414:19
 */
public class MultiDialectDaoProxyFactory<T> {
    private Map<Method, DaoMethodExecutor> methodCache = new ConcurrentHashMap(5000);
    private Class<T> daoInterface;

    public MultiDialectDaoProxyFactory(Class<T> daoInterface) {
        this.daoInterface = daoInterface;
    }

    protected T newInstance(MultiDialectDaoInvocationHandler<T> handler) {
        return (T) Proxy.newProxyInstance(daoInterface.getClassLoader(),
                new Class[]{daoInterface}, handler);
    }

    public T newInstance(JdbcOperations jdbcOperations) {
        final MultiDialectDaoInvocationHandler<T> handler =
                new MultiDialectDaoInvocationHandler<T>(jdbcOperations, methodCache);
        return newInstance(handler);
    }
}
